import calendar
import datetime
import  time
def getBetweenMonth(begin_date):
    date_list = []
    begin_date = datetime.datetime.strptime(begin_date, "%Y-%m-%d")
    end_date = datetime.datetime.strptime(time.strftime('%Y-%m-%d', time.localtime(time.time())), "%Y-%m-%d")
    while begin_date <= end_date:
        date_str = begin_date.strftime("%Y-%m")
        date_list.append(date_str)
        begin_date = add_months(begin_date, 1)
    return date_list


def add_months(dt, months):
    month = dt.month - 1 + months
    year = int(dt.year + month / 12)
    month = month % 12 + 1
    day = min(dt.day, calendar.monthrange(year, month)[1])
    return dt.replace(year=year, month=month, day=day)


def getBetweenQuarter(begin_date):
    quarter_list = []
    month_list = getBetweenMonth(begin_date)
    for value in month_list:
        tempvalue = value.split("-")
        if tempvalue[1] in ['01', '02', '03']:
            quarter_list.append(tempvalue[0] + "-03-31")
        elif tempvalue[1] in ['04', '05', '06']:
            quarter_list.append(tempvalue[0] + "-06-30")
        elif tempvalue[1] in ['07', '08', '09']:
            quarter_list.append(tempvalue[0] + "-09-30")
        elif tempvalue[1] in ['10', '11', '12']:
            quarter_list.append(tempvalue[0] + "-12-31")
    quarter_set = set(quarter_list)
    quarter_list = list(quarter_set)
    quarter_list.sort()
    return quarter_list

def getBeforeMonth(timestamp,months):
    '''
    后退 几个月
    '''
    from calendar import monthrange
    now_time = datetime.datetime.strptime(timestamp,'%Y-%m-%d')
    year,month,day = [int(one) for one in str(now_time).split(' ')[0].split('-')]
    for i in range(months):
        now_time-=datetime.timedelta(days=monthrange(year,month)[1])
    next_timestamp=now_time.strftime('%Y-%m-%d')
    return next_timestamp



def getBeforeMonth(timestamp,months):#format='%Y-%m-%d %H:%M:%S'):
    '''
    以给定时间戳为基准，后退 months 个月后得到对应的时间戳
    '''
    from calendar import monthrange
    now_time=datetime.datetime.strptime(timestamp,'%Y-%m-%d')
    year,month,day=[int(one) for one in str(now_time).split(' ')[0].split('-')]
    for i in range(months):
        now_time-=datetime.timedelta(days=monthrange(year,month)[1])
    next_timestamp=now_time.strftime('%Y-%m-%d')
    tempvalue = next_timestamp.split("-")
    if tempvalue[1] in ['01', '02', '03']:
        return (tempvalue[0] + "-03-31")
    elif tempvalue[1] in ['04', '05', '06']:
        return (tempvalue[0] +  "-06-30")
    elif tempvalue[1] in ['07', '08', '09']:
        return (tempvalue[0] +  "-09-30")
    elif tempvalue[1] in ['10', '11', '12']:
        return (tempvalue[0] +  "-12-31")
    return next_timestamp

dateList1 = ['2014-03-31', '2014-06-03', '2014-06-30', '2014-09-30', '2014-12-01', '2014-12-31', '2015-03-31',
            '2015-05-14', '2015-06-30', '2015-09-30', '2015-11-30', '2015-12-31', '2016-03-31', '2016-05-30', '2016-06-30',
            '2016-09-30', '2016-12-28', '2016-12-31', '2017-03-31', '2017-05-31', '2017-06-30', '2017-09-30', '2017-12-31',
            '2018-03-31', '2018-05-28', '2018-06-30', '2018-09-30', '2018-12-03', '2018-12-31', '2019-03-31', '2019-06-30']

for date in dateList1:
    print(getBeforeMonth(date,3))